package com.soft.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.soft.entity.Dept;
import com.soft.entity.Location;
import com.soft.mapper.DeptMapper;
import com.soft.mapper.LocationMapper;

@Service
public class LocationService {
	@Autowired
	private DeptMapper deptMapper;

	@Autowired
	private LocationMapper locationMapper;
	public List<Location> queryByDeptLoc(String deptLoc){
		
		QueryWrapper<Dept> deptWrapper = new QueryWrapper<>();
		deptWrapper.eq("dept_loc", deptLoc);
		Dept dept = deptMapper.selectOne(deptWrapper);
		QueryWrapper<Location> locationWrapper =new QueryWrapper<>();
		locationWrapper.eq("deptno", dept.getDeptno());
		List<Location> locationList = locationMapper.selectList(locationWrapper);
		return locationList;
	}
	public int insertLocation(String loc_code,String loc_country,String deptno) {
		QueryWrapper<Dept> deptWrapper = new QueryWrapper<>();
		deptWrapper.eq("deptno", deptno);
		Dept dept = deptMapper.selectOne(deptWrapper);
		Location location = new Location();
		location.setLocCode(loc_code);
		location .setLocCountry(loc_country);
		location.setDeptno(dept.getDeptno());
		int num = locationMapper.insert(location);
		return num;
	}
	public int updateLocation(String loc_code,String loc_country,String deptno) {
		QueryWrapper<Dept> deptWrapper = new QueryWrapper<>();
		deptWrapper.eq("deptno", deptno);
		Dept dept = deptMapper.selectOne(deptWrapper);
		UpdateWrapper<Location> locationWrapper = new UpdateWrapper<>();
		locationWrapper.eq("loc_code", loc_code);
		locationWrapper.eq("deptno",dept.getDeptno());
		Location location = new Location();
		location.setLocCountry(loc_country);
		int num = locationMapper.update(location,locationWrapper);
		return num;
	}
}
